// pages/shopQuickList/shopQuickList.js
const util = require('../../utils/util.js')
const app = getApp();
import Toast from '../../dist/toast/toast';

const cloudUrl = app.globalData.cloudUrl;
const tempUrl = cloudUrl+"shopItem1.png";
const tempUrl2 = cloudUrl+"shopItem2.png";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cloudUrl:cloudUrl,
    allShops:[],
    allShopSortType:0,//0普通id排序，1销量高，2销量低，3价格低，4价格高
    allSortColors:[{id:1,select:-1,color1:"#000000", color2:"#d7d7d7", color3:"#999999"},
                  {id:2,select:-1,color1:"#000000", color2:"#d7d7d7", color3:"#999999"}],
    allShopList:[
      {id:1,name:"松阳茶叶",img:tempUrl,txtColor:"#000000",hot:100, price:129, oldPrice:300},
      {id:2,name:"松阳文创",img:tempUrl2,txtColor:"#000000",hot:98, price:56, oldPrice:888},
      {id:3,name:"松阳特产",img:tempUrl2,txtColor:"#000000",hot:20, price:12, oldPrice:300},
      {id:4,name:"松阳特产",img:tempUrl,txtColor:"#000000",hot:45, price:63, oldPrice:300},
      {id:5,name:"松阳特产",img:tempUrl,txtColor:"#000000",hot:4, price:75, oldPrice:300},
      {id:6,name:"松阳特产",img:tempUrl2,txtColor:"#000000",hot:57, price:258, oldPrice:300},
      {id:7,name:"丽水好物",img:tempUrl,txtColor:"#000000",hot:8, price:21, oldPrice:300}],
    allSortList:[],
    isShow2:true,
    isTipsShow:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.onShopsRefresh();
  },
  onQuickClick(e){
    this.setData({isTipsShow:!this.data.isTipsShow});
  },
  onNotesEvent(e){
    //console.log(e);
    if(e.detail.type == "close"){
      this.setData({isTipsShow:false});
    }
    else if(e.detail.type == "confirm"){
      if(e.detail.select && e.detail.num > 0){
        this.setData({isTipsShow:false});
        //do something...
      }
      else{
        Toast("请选择商品规格和属性");
      }
    }
  },
  onShopDetailsClick(e){
    util.gotoShopDetailsPage(e);
  },
  onShowClick(e){
    this.setData({isShow2:!this.data.isShow2});
  },
  onSortClick(e){
    const idx = e.currentTarget.dataset.index;
    //color1:"#000000", color2:"#d7d7d7", color3:"#999999"
    var infos = this.data.allSortColors;
    var info = null;
    for(var i=0; i<infos.length; i++){
      info = infos[i];
      if(idx == i){
        if(info.select == 0){
          info.select = 1;
          info.color1 = "#6699ff";
          info.color3 = "#6699ff";
          info.color2 = "#a0c0ff";
        }
        else{
          info.select = 0;
          info.color1 = "#6699ff";
          info.color2 = "#6699ff";
          info.color3 = "#a0c0ff";
        }
        this.data.allShopSortType = i * 2 + 1 + info.select;
      }
      else{
        info.select = -1;
        info.color1 = "#000000";
        info.color2 = "#d7d7d7";
        info.color3 = "#999999";
      }
    }
    //console.log(this.data.allShopSortType);
    this.setData({allSortColors:this.data.allSortColors});
    this.onShopsRefresh();
  },
  onShopsRefresh(){
    var arr = this.data.allShopList.concat();
    const sortType = this.data.allShopSortType;
    if(sortType == 0){
      arr = util.sortArray(arr, "id");
    }
    else if(sortType == 1){
      arr = util.sortArray(arr, "hot", true);
    }
    else if(sortType == 2){
      arr = util.sortArray(arr, "hot");
    }
    else if(sortType == 3){
      arr = util.sortArray(arr, "price");
    }
    else if(sortType == 4){
      arr = util.sortArray(arr, "price", true);
    }
    for(var i=0; i<arr.length; i++){
      if(i % 2 == 0){
        arr[i].posx = 0;
      }
      else{
        arr[i].posx = 53;
      }
      arr[i].posy = 20 + Math.floor(i / 2) * 540;
    }
    this.setData({allSortList:arr});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})